package org.dream.ui;

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import org.dream.domain.Stock;
import org.dream.domain.ZtAnalysis;
import org.dream.service.StockService;
import org.dream.service.StockServiceImpl;
import org.dream.service.SzzsService;
import org.dream.ui.chart.impl.KChart;

/**
 * 股票列表面板
 * 
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class ZtTablePanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private SzzsService szzsService = new SzzsService();

	public ZtTablePanel() {
		super(new GridLayout(2, 2));
		// addColumnNames();// 添加表格列名称
		// addStockKindBox(); // 添加股票分类Box
		for(int i=0;i<4;i++){
			add(new JScrollPane(new ZtTable(szzsService.getPreDay(i))));
		}
		
		//add(new JScrollPane(new ZtTable("2011/08/10")));
		//add(new JButton("3"));
		//add(new JButton("4"));

	}

}

class ZtTable extends JTable {
	private StockService stockService = new StockServiceImpl();
	private static final int ROWHEIGHT = 21;
	private String day;

	public ZtTable(String day) {
		this.day = day;
		TableModel tableModel = new DefaultTableModel(getStockTableRowData(), addColumnNames());
		this.setModel(tableModel);
		this.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (ZtTable.this.getSelectedRow() == -1) {
					return;
				}
				// 股票代码
				String stockCode = (String) ZtTable.this.getModel().getValueAt(ZtTable.this.getSelectedRow(), 1);
				Stock stock = stockService.getStockByCode(stockCode);
				switch (ZtTable.this.getSelectedColumn()) {
					case 2:// 查看K线
						MainFrame.getInstanse().setCenterPanel(new KChart(stockCode, null, null));
						break;
				}

			}
		});
		this.setRowHeight(ROWHEIGHT);
		JTableUtil.fitTableColumns(this);
	}

	/**
	 * 得到股票表格数据
	 */
	public Vector<Vector<Object>> getStockTableRowData() {
		Vector<Vector<Object>> data = new Vector<Vector<Object>>();
		List<ZtAnalysis> stockList = stockService.findZtAnalysis(day);

		for (int i = 0; i < stockList.size(); i++) {
			Vector<Object> row = new Vector<Object>();
			row.add("" + (i + 1));
			row.add(stockList.get(i).getStockCode());
			row.add(stockList.get(i).getStockName());
			row.add(stockList.get(i).getRs2());
			row.add(stockList.get(i).getRs3());
			row.add(stockList.get(i).getRs4());
			row.add(stockList.get(i).getRs5());
			data.add(row);
		}
		return data;
	}

	/**
	 * 添加表格列名称
	 */
	private Vector<String> addColumnNames() {
		Vector<String> columnNames;
		columnNames = new Vector<String>();
		columnNames.add("序号");
		columnNames.add("代码");
		columnNames.add("2011-8-9");
		columnNames.add("第二日");
		columnNames.add("第三日");
		columnNames.add("第四日");
		columnNames.add("第五日");
		return columnNames;
	}

}
